# ------- SEG 1 -------
import numpy as np
import pandas as pd
df = pd.read_csv("bank.csv")
cols = df.columns.values
cols = [col.replace("\"","") for col in cols[0].split(";")]
print(cols)

# ------- SEG 2 -------
datas = []
for i in range(len(df)):
    s = df.iloc[i].values[0]
    datas.append([item.replace("\"","") for item in s.split(";")])
datas = np.array(datas)
print(datas)

# ------- SEG 3 -------
df_frame = {}
for i in range(len(cols)):
    df_frame[cols[i]] = datas[:,i]
df = pd.DataFrame(df_frame)
print(df)

# ------- SEG 4 -------
df[["age","balance","duration","campaign","pdays","previous"]] = df[["age","balance","duration","campaign","pdays","previous"]].astype(int)
print(df.dtypes)

# ------- SEG 5 -------
print(df["age"].value_counts().sort_index().plot.line())
df["age"].value_counts().sort_index().head(25).plot.bar()

# ------- SEG 6 -------
df.duplicated()
df.drop_duplicates()
class_mapping = {"no":0,"yes":1}
df["default"] = df["default"].map(class_mapping)
df["housing"] = df["housing"].map(class_mapping)
df["loan"] = df["loan"].map(class_mapping)
df["y"] = df["y"].map(class_mapping)

# ------- SEG 7 -------
month_dict = {'oct': '10','may': '05', 'apr': '04', 'jun': '06', 'feb': '02', 'aug': '08',
              'jan': '01', 'jul': '07', 'nov': '11','sep': '09', 'mar': '03', 'dec': '12'}
df["month"] = df["month"].map(month_dict)
df["date"] = "2019" + "-" + df["month"] + "-" + df["day"]
df["date"] = pd.to_datetime(df["date"],format="%Y-%m-%d")
df["date"] = pd.to_datetime("2020-01-01",format="%Y-%m-%d") - df["date"]
df["date"] = df["date"].dt.days
print(df[["date"]])
del(df["day"])
del(df["month"])

# ------- SEG 8 -------
jobs = df["job"].unique()
job_mapping = {jobs[i]:i for i in range(jobs.shape[0])}

maritals = df["marital"].unique()
marital_mapping = {maritals[i]:i for i in range(maritals.shape[0])}

educations = df["education"].unique()
education_mapping = {educations[i]:i for i in range(educations.shape[0])}

contacts = df["contact"].unique()
contact_mapping = {contacts[i]:i for i in range(contacts.shape[0])}

poutcomes = df["poutcome"].unique()
poutcome_mapping = {poutcomes[i]:i for i in range(poutcomes.shape[0])}

df["marital"] = df["marital"].map(marital_mapping)
df["job"] = df["job"].map(job_mapping)
df["education"] = df["education"].map(education_mapping)
df["contact"] = df["contact"].map(contact_mapping)
df["poutcome"] = df["poutcome"].map(poutcome_mapping)
print(df)

# ------- SEG 9 -------
bins = [18,25,35,45,55,100]
df["age"] = pd.cut(df["age"],bins,labels=False)
bins = [-np.inf, 4137.1, 11587.2, np.inf]
df['balance'] = pd.cut(df['balance'], bins, labels=False)
print(df[["age","balance"]])

# ------- SEG 10 -------
cols = ["pdays","duration","campaign","date"]
for col in cols:
    df[col] = (df[col] - df[col].min()) / (df[col].max() - df[col].min())
print(df[["pdays","duration","campaign","date"]])
df.to_csv("after_bank.csv")

# ------- SEG 11 -------
from sklearn.tree import DecisionTreeClassifier as DTC,export_graphviz
df = pd.read_csv("after_bank.csv")
df = df.iloc[:,1:]
cols = list(df.columns.values)
cols.remove("y")
X = df[cols]
y = df[["y"]]

# ------- SEG 12 -------
X_train = X[:4000]
y_train = y[:4000]
X_test = X[4000:5000]
y_test = y[4000:5000]
dtc = DTC(criterion="entropy",max_depth=5)
dtc.fit(X_train,y_train)
print("准确率:",dtc.score(X_test,y_test))

# ------- SEG 13 -------
# DecisionTreeClassifier(
#     criterion="gini",
#     splitter="best",
#     max_depth=None,
#     min_samples_split=2,
#     min_samples_leaf=1,
#     min_weight_fraction_leaf=0.,
#     max_features=None,
#     random_state=None,
#     max_leaf_nodes=None,
#     min_impurity_decrease=0.,
#     min_impurity_split=None,
#     class_weight=None,
#     presort=False
# )
dtc = DTC(criterion="gini",max_depth=5)
dtc.fit(X_train,y_train)
print("准确率:",dtc.score(X_test,y_test))
for depth in range(1,10):
    dtc = DTC(criterion="gini",splitter="random",max_depth=depth)
    dtc.fit(X_train,y_train)
    print("depth:",depth,"|","准确率:",dtc.score(X_test,y_test))

# # ------- SEG 14 -------
# from IPython.display import Image
# from sklearn import tree
# from sklearn import GridSearchCV
# import pydotplus
# import os
# os.environ["PATH"] += os.pathsep + "C:/Program Files/Graphviz 2.44.1/bin/"
# param_dist = {
#     "max_depth":range(1,10,1),
#     "min_samples_split":range(1,5,1)
# }
# grid = GridSearchCV(
#     dtc,
#     param_dist,
#     cv=3,
#     n_iter=300
# )
# grid.fit(X_train,y_train)
# best = grid.best_estimator_
# print(best)
# dot_data = tree.export_graphviz(
#     dtc,
#     out_file=None,
#     feature_names=X_train.columns,
#     class_names=["1","0"],
#     filled=True,
#     rounded=True,
#     special_characters=True
# )
# graph = pydotplus.graph_from_dot_data(dot_data)
# graph.write_png("DecisionTree.png")
